﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkBasket.Api;

namespace Workbasket.CoreExtensions.Controls
{
    [Export(typeof(IControl))]

    class TextBox : IControl
    {
        static private Dictionary<string, string> _Properties = new Dictionary<string, string>();

        public TextBox()
        {
            _Properties.Add("ID", "");
            _Properties.Add("type", "text,checkbox,range,email,number,radio,url");
            _Properties.Add("PlaceHolder", "");
            _Properties.Add("Label", "");
            _Properties.Add("Field", "");
            _Properties.Add("Required", "false,true");
            _Properties.Add("Minlength", "");
            _Properties.Add("Maxlength", "");
            _Properties.Add("Pattern", "");
            _Properties.Add("Readonly", "false,true");
        }

 
        public string RenderBeginTag(IInstance InstanceData)
        {
            StringBuilder sb = new StringBuilder();
    

         

            sb.Append("<label for='" + InstanceData.Properties["ID"] + "' class='label label-default'>" + InstanceData.Properties["Label"] + "</label>");

            if (InstanceData.Properties["Readonly"] == "false") 
            {
                sb.Append("<input type='" + InstanceData.Properties["type"] + "' class='form-control' id='" + InstanceData.Properties["ID"] 
                                            + "' placeholder='" + InstanceData.Properties["PlaceHolder"] 
                                          + "' ng-model='InstanceData." + InstanceData.Properties["Field"] + "' ");

                if (InstanceData.Properties["Required"].Length > 0)
                {
                    sb.Append(" required ");
                }
                if (InstanceData.Properties["Minlength"].Length > 0)
                {
                    sb.Append(" ng-minlength='" + InstanceData.Properties["Minlength"] + "' ");
                }

                if (InstanceData.Properties["Maxlength"].Length > 0)
                {
                    sb.Append(" ng-maxlength='" + InstanceData.Properties["Maxlength"] + "' ");
                }
                if (InstanceData.Properties["Pattern"].Length > 0)
                {
                    sb.Append(" ng-pattern='" + InstanceData.Properties["Pattern"] + "' ");
                }

              
                sb.Append(">");

            } else {


                sb.Append("<label for='" + InstanceData.Properties["ID"] + "' class='form-control' " +
                                " ng-model='InstanceData." + InstanceData.Properties["Field"] + "' >" +
                                  "</label>");

            }
            
          

            return sb.ToString();
        }

        public string RenderEndTag()
        {
            return "";
        }

        public string ControlName
        {
            get { return "TextBox"; }
        }
        public string ToolBoxIcon
        {
            get { return "glyphicon glyphicon-edit"; }
        }
        public string Description
        {
            get {
                return "";
            
            }
        }

        IDictionary<string, string> IControl.Properties
        {
            get
            {
                return _Properties;
            }
        }
    }
}
